package com.yxx.servicealbum.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.model.album.AlbumAttributeValue;
import com.yxx.model.album.AlbumInfo;
import com.yxx.query.album.AlbumInfoQuery;
import com.yxx.result.Result;
import com.yxx.servicealbum.service.AlbumInfoService;
import com.yxx.util.AuthContextHolder;
import com.yxx.vo.album.AlbumAttributeValueVo;
import com.yxx.vo.album.AlbumInfoVo;
import com.yxx.vo.album.AlbumListVo;
import com.yxx.vo.album.AlbumStatVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yuxiangxun
 * @date 2025/3/16
 * @apiNote
 */
@RestController
@Slf4j
@RequestMapping("/api/album/albumInfo")
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @PostMapping("/saveAlbumInfo")
    @Operation(summary = "保存专辑信息")
    public Result save(@RequestBody @Validated AlbumInfoVo albumInfoVo){
        albumInfoService.saveAlbumInfo(albumInfoVo, AuthContextHolder.getUserId());
        return Result.ok();
    }

    @PostMapping("/findUserAlbumPage/{page}/{limit}")
    @Operation(summary = "分页查询用户专辑")
    public Result findUserAlbumPage(
            @Parameter(name = "page", description = "当前页码", required = true) @PathVariable Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true) @PathVariable Long limit,
            @Parameter(name = "albumInfoQuery",description = "查询对象") @RequestBody AlbumInfoQuery albumInfoQuery
    ){
        // 获取当前用户
        albumInfoQuery.setUserId(AuthContextHolder.getUserId());
        Page<AlbumListVo> albumInfoPage = new Page<>(page, limit);
        IPage<AlbumListVo> iPage = this.albumInfoService.findUserAlbumPage(albumInfoPage, albumInfoQuery);
        return Result.ok(iPage);
    }


    @Operation(summary = "删除专辑信息")
    @DeleteMapping("/removeAlbumInfo/{id}")
    public Result removeAlbumInfoById(@PathVariable Long id) {
        albumInfoService.removeAlbumInfoById(id);
        return Result.ok();
    }

    /**
     * 根据id 获取到专辑信息
     * @param id
     * @return
     */
    @Operation(summary = "获取专辑信息")
    @GetMapping("/getAlbumInfo/{id}")
    public Result<AlbumInfo> getAlbumInfo(@PathVariable Long id) {
        //	调用服务层方法
        AlbumInfo albumInfo = albumInfoService.getAlbumInfoById(id);
        return Result.ok(albumInfo);
    }

    @Operation(summary = "修改专辑")
    @PutMapping("/updateAlbumInfo/{id}")
    public Result updateById(@PathVariable Long id,@RequestBody @Validated AlbumInfoVo albumInfoVo){
        //	调用服务层方法
        albumInfoService.updateAlbumInfo(id,albumInfoVo);
        return Result.ok();
    }

    /**
     * 获取当前用户专辑列表
     * @return
     */
    @Operation(summary = "获取当前用户全部专辑列表")
    @GetMapping("/findUserAllAlbumList")
    public Result findUserAllAlbumList() {
        //	调用服务层方法
        List<AlbumInfo> list = albumInfoService.findUserAllAlbumList(AuthContextHolder.getUserId());
        return Result.ok(list);
    }

    @Operation(summary = "获取专辑属性列表")
    @GetMapping("/findAlbumAttributeValue/{albumId}")
    public Result<List<AlbumAttributeValue>> finddAlbumAttributeValue(@PathVariable Long albumId){
        List<AlbumAttributeValue> albumAttributeValueList = albumInfoService.findAlbumAttributeValueByAlbumId(albumId);
        return Result.ok(albumAttributeValueList);
    }

    /**
     * 通过专辑Id 获取到专辑状态信息
     * @param albumId
     * @return
     */
    @GetMapping("/getAlbumStatVo/{albumId}")
    Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId){
        //	获取服务层方法
        AlbumStatVo albumStatVo = this.albumInfoService.getAlbumStatVoByAlbumId(albumId);
        return Result.ok(albumStatVo);
    }
}
